<template>
  <view class="container">
    <!-- 功能1：选择本机图片并预览、显示 -->
    <view class="image-section">
      <!-- 图片预览区域 -->
      <image 
        :src="imagePath" 
        mode="aspectFill" 
        class="preview-image"
        v-if="imagePath"
      ></image>
      <view class="no-image" v-else>未选择图片</view>
      
      <!-- 操作按钮 -->
      <button @click="chooseImage" class="operate-btn">选择图片</button>
      <button @click="previewImage" class="operate-btn" v-if="imagePath">预览图片</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      imagePath: '' 
    }
  },
  methods: {
  
    chooseImage() {
      uni.chooseImage({
        count: 1, 
        sourceType: ['album'], 
        success: (res) => {
        
          this.imagePath = res.tempFilePaths[0]
        },
        fail: (err) => {
          console.log('选择图片失败：', err)
        }
      })
    },
    
    
    previewImage() {
      if (!this.imagePath) return
      
      uni.previewImage({
        current: this.imagePath, 
        urls: [this.imagePath], 
        success: () => {
          console.log('预览图片成功')
        },
        fail: (err) => {
          console.log('预览图片失败：', err)
        }
      })
    }
  }
}
</script>

<style>
.container {
  padding: 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.image-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.preview-image {
 width: 400rpx;  
  height: 400rpx;
  border-radius: 50%;  
  border: 2px solid #eee;
  margin-bottom: 30rpx;
  object-fit: cover; 
}

.no-image {
   width: 400rpx;
   height: 400rpx;
   border-radius: 50%; 
   border: 2px dashed #ccc;
   margin-bottom: 30rpx;
   display: flex;
   justify-content: center;
   align-items: center;
   color: #999;
   font-size: 28rpx;
   text-align: center;
   padding: 20rpx;
}

.operate-btn {
  width: 600rpx;
  margin-bottom: 20rpx;
}
</style>